package UI;

import DAO.AccountDao.Accounts_Database;
import DAO.Goods;
import DAO.GoodsDaoImpl;
import DAO.AccountDao.Accounts_Database;
import DAO.Goods;
import DAO.GoodsDaoImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

public class Login {

    public static void main(String[] args) {
        // 创建 JFrame 实例
        JFrame frame = new JFrame("登录页面");
        // Setting the width and height of frame
        frame.setSize(350, 200);
        frame.setLocation(500,250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* 创建面板 */
        JPanel panel = new JPanel();
        // 添加面板
        frame.add(panel);
        /*
         * 调用用户定义的方法并添加组件到面板
         */
        placeComponents(panel);

        // 设置界面可见
        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {

        /*
         * 设置布局为 null
         */
        panel.setLayout(null);

        // 创建 JLabel
        JLabel userLabel = new JLabel("用户名:");
        /* 这个方法定义了组件的位置。
         * setBounds(x, y, width, height)
         * x 和 y 指定左上角的新位置，由 width 和 height 指定新的大小。
         */
        userLabel.setBounds(10,20,80,25);
        panel.add(userLabel);

        /*
         * 创建文本域用于用户输入
         */
        JTextField userText = new JTextField(20);
        userText.setBounds(100,20,165,25);
        panel.add(userText);

        // 输入密码的文本域
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(10,50,80,25);
        panel.add(passwordLabel);

        /*
         *这个类似用于输入的文本域
         * 但是输入的信息会以点号代替，用于包含密码的安全性
         */
        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100,50,165,25);
        panel.add(passwordText);

        // 创建登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setBounds(10, 80, 80, 25);
        panel.add(loginButton);
        loginButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                //创建账户数据类
                Accounts_Database account=new Accounts_Database();
                //将文本框中账号和密码赋值给account，再进行比对
                account.setUserName(userText.getText());
                account.setUserPassword(String.valueOf(passwordText.getPassword()));
                try {
                    //System.out.println(userText.getText());
                   // System.out.println(String.valueOf(passwordText.getPassword()));
                    if(account.checkAccout()){
                        new Index();

                    }
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });

        //创建注册按钮
        JButton cancelButton = new JButton("注册");
        cancelButton.setBounds(100, 80, 80, 25);
        panel.add(cancelButton);
        cancelButton.addActionListener(new ActionListener(){
            //注册按钮单击事件
            @Override
            public void actionPerformed(ActionEvent e) {
                //创建账户数据类
                Accounts_Database account=new Accounts_Database();
                //将文本框中账号和密码赋值给account，再调用创建方法
                account.setUserName(userText.getText());
                account.setUserPassword(String.valueOf(passwordText.getPassword()));
                try {
                    account.CreatAccount();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                //System.exit(0);//关闭虚拟机
            }
        });
    }

}

class Index {
    private static final int WIDTH = 300;

    private static final int HEIGHT = 200;

    public Index() {
         JFrame jf = new JFrame("商品信息管理系统");
        jf.setBounds(350, 200, 800, 500);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 添加面板
        jf.setVisible(true);
        jf.setLayout(null);

        JPanel contentPane = new JPanel();
        jf.setContentPane(contentPane);
        //添加goodsNum查找框
        JTextField queryNum = new JTextField(20);
        queryNum.setText("该处输入待查找/删除/修改的编号：");

        queryNum.setBounds(100,20,165,25);
        contentPane.add(queryNum);
        JButton query=new JButton("查找");
        contentPane.add(query);
        query.setBackground(Color.PINK);
        JButton add=new JButton("增加");
        contentPane.add(add);
        add.setBackground(Color.PINK);
        JButton modify=new JButton("修改");
        contentPane.add(modify);
        modify.setBackground(Color.PINK);
        JButton delete=new JButton("删除");
        contentPane.add(delete);
        delete.setBackground(Color.PINK);
        JButton load=new JButton("加载");
        contentPane.add(load);
        load.setBackground(Color.PINK);

        JTextArea jta=new JTextArea("该处显示查找内容",1,30);
        jta.setLineWrap(true);    //设置文本域中的文本为自动换行
        jta.setForeground(Color.BLACK);    //设置组件的背景色
        jta.setFont(new Font("楷体",Font.BOLD,16));    //修改字体样式
        jta.setBackground(Color.pink);    //设置按钮背景色
        contentPane.add(jta);
        //文本框区域
        Object[][] tableDate=new Object[100][6];
        for(int i=0;i<20;i++)
        {
            tableDate[i][0]="";
            for(int j=1;j<6;j++)
            {
                tableDate[i][j]="";
            }
        }

        String[] name={"编号","品名","单价","上市日期","类别","产地"};
        JTable table=new JTable(tableDate,name);
        contentPane.add(new JScrollPane(table));




        //加载按钮监听事件
        load.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GoodsDaoImpl goodsDao= new GoodsDaoImpl();
                ArrayList<Goods> list=new ArrayList<Goods>();
                list=goodsDao.findAll();
               // DefaultTableModel model=(DefaultTableModel) table.getModel();    //获得表格模型
                int num=1;
                //先清空
                for(int i=0;i<100;i++)
                {
                    tableDate[i][0]="";
                    for(int j=1;j<6;j++)
                    {
                        tableDate[i][j]="";
                    }
                }
                //再读取数据库中数据
                for(Goods g:list)
                {
                    tableDate[num][0]=g.getGoodsNum();
                    tableDate[num][1]=g.getGoodsName();
                    tableDate[num][2]=g.getGoodsPrice();
                    tableDate[num][3]=g.getGoodsListingDate();
                    tableDate[num][4]=g.getGoodsCategories();
                    tableDate[num][5]=g.getGoodsOrigin();
                    num++;
                }
                table.repaint();
            }
        });

        //查找监听事件
        query.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GoodsDaoImpl goodsDao= new GoodsDaoImpl();
                String goodsNum=new String();
                goodsNum=(queryNum.getText());
                Goods goods;
                goods=goodsDao.findByGoodsNum(goodsNum);
                jta.setText(goods.getGoodsNum()+"\n"+goods.getGoodsName()+"\n"+goods.getGoodsPrice()+"\n"+goods.getGoodsListingDate()+"\n"+goods.getGoodsCategories()+"\n"+goods.getGoodsOrigin());
            }
        });

        //删除监听
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GoodsDaoImpl goodsDao= new GoodsDaoImpl();
                String goodsNum=new String();
                goodsNum=(queryNum.getText());
                int result=goodsDao.delete(goodsNum);

            }
        });

        //修改事件
        modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GoodsDaoImpl goodsDao= new GoodsDaoImpl();
                String goodsNum=new String();
                goodsNum=(queryNum.getText());
                Goods g=goodsDao.findByGoodsNum(goodsNum);
                g.setGoodsNum(tableDate[0][0].toString());
                g.setGoodsName(tableDate[0][1].toString());
                g.setGoodsPrice(tableDate[0][2].toString());
                g.setGoodsListingDate(tableDate[0][3].toString());
                g.setGoodsCategories(tableDate[0][4].toString());
                g.setGoodsOrigin(tableDate[0][5].toString());
                goodsDao.updata(g);

            }
        });
//增添事件
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GoodsDaoImpl goodsDao= new GoodsDaoImpl();
                Goods g=new Goods();
                g.setGoodsNum(tableDate[0][0].toString());
                g.setGoodsName(tableDate[0][1].toString());
                g.setGoodsPrice(tableDate[0][2].toString());
                g.setGoodsListingDate(tableDate[0][3].toString());
                g.setGoodsCategories(tableDate[0][4].toString());
                g.setGoodsOrigin(tableDate[0][5].toString());
                ArrayList<Goods> gg=new ArrayList<>();
                gg=goodsDao.findAll();
                boolean flag=true;
                for(Goods gds:gg)
                {
                    if(gds.getGoodsNum().equals(g.getGoodsNum()))
                    {
                        flag=false;
                        break;
                    }
                }
                if(flag)
                goodsDao.insert(g);
                else
                    new addproblem();

            }
        });


    }
    class addproblem{
        public addproblem() {
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500, 250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JLabel j = new JLabel("该编号已存在，请输入正确的编号！");
            frame.add(j);

        }
        }

}
